

package us.gaaoc.framework.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import us.gaaoc.framework.dao.persistenceLogic.BaseDataAccessor;
import us.gaaoc.framework.model.util.CaseRecordsCountPerDay;
import us.gaaoc.framework.model.util.CaseRecordsCountPerMonth;
import us.gaaoc.framework.model.util.DocumentsCountPerMonth;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.CaseStatistics;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.CaseStatisticsByDay;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.CaseStatisticsByMonth;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.DocumentStatistics;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.FilingStatistics;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.PersonStatistics;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.SignatureStatistics;
import us.gaaoc.framework.webapp.backingbeans.agent.reports.SystemStatistics;

public class ReportChartUtil extends BaseDataAccessor  {
	//jogi
	static String reportImagePath = new StringBuilder(((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRealPath("/")).append("/images/reports").toString();//"//srv//apache//webapps//GAJE_2.0//images//reports";
	
	public String getReportImagePath() {
		File dir=new File(reportImagePath);
		if(!dir.exists())
			dir.mkdirs();
		return reportImagePath;
	}
	
	public void setReportImagePath(String path) {
		reportImagePath = path;
	}
	
	 public String caseStatistics() {
		 
		   // create the CaseStatisticsChart
		   
		     CaseStatistics caseStatusReport = new CaseStatistics();
		     // Create pie chart
	         DefaultPieDataset pieDataset = new DefaultPieDataset();
	         pieDataset.setValue("Contempt Cases="+caseStatusReport.getNumberOfContemptCases()*100/caseStatusReport.getNumberOfCases()+"%",caseStatusReport.getNumberOfContemptCases()*100/caseStatusReport.getNumberOfCases());
		     pieDataset.setValue("Establishment Cases="+caseStatusReport.getNumberOfEstablishedCases()*100/caseStatusReport.getNumberOfCases()+"%", caseStatusReport.getNumberOfEstablishedCases()*100/caseStatusReport.getNumberOfCases());
	  
	        String path = getReportImagePath();
	        //String path = "//home//koshyarb//workspace//GAJE_BibbCounty_2.0/images";
	       
	         final JFreeChart chart = ChartFactory.createPieChart("Case StatisTics Chart",pieDataset, true,true,false);
	       
	         //saving the chart as png file
	         try {
	             ChartUtilities.saveChartAsPNG(new File(path+"/CaseStatisticsChart.png"), chart, 600,
	                 500);
	         }
	             catch (Exception e) {
	                 System.out.println("Problem occurred creating chart.");
	             }	
	             return path;
	            
	        }
	 
	 	public String caseStatisticsByDay()
	 	{
	 		 
	         // Create Bar chart
	    	 CaseStatisticsByDay caseStatByDay = new  CaseStatisticsByDay();
	         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	         List<CaseRecordsCountPerDay> caseList = new ArrayList<CaseRecordsCountPerDay>();
	         caseList = caseStatByDay.getNumberOfCasesByDay();
	        String path = getReportImagePath();
	        //images//reports";
	        // String path = "//home//koshyarb//workspace//GAJE_BibbCounty_2.0/images";
	        for(CaseRecordsCountPerDay caseRecordsCountPerDay : caseList){
	        	dataset.setValue(caseRecordsCountPerDay.getCount(), "Cases",caseRecordsCountPerDay.getDay());
	        }
	        ///Dataset
	        JFreeChart chart = ChartFactory.createBarChart("Case Statistics By Day",
	                "Days", "Cases", dataset, PlotOrientation.VERTICAL, true,
	                true, false);
	        try {
	            ChartUtilities.saveChartAsPNG(new File(path+"/CaseStatisticsByDay.png"), chart, 800, 500);
	            } catch (IOException e) {
	            System.err.println("Problem occurred creating chart.");
	        }
	        return path;
	    }
	 	public String caseStatisticsByMonth()
		 {
			 CaseStatisticsByMonth  caseStatByMonth = new CaseStatisticsByMonth();
			 // Create a simple Bar chart
	         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	         List<CaseRecordsCountPerMonth> monthList = new ArrayList<CaseRecordsCountPerMonth>();
	         monthList = caseStatByMonth.getNumberOfCasesByMonth();
	         for(CaseRecordsCountPerMonth caseRecordsCountPerMonth : monthList){
	         	dataset.setValue(caseRecordsCountPerMonth.getCount(), "Cases",caseRecordsCountPerMonth.getMonth());
	         }
	         String path = getReportImagePath();
	         JFreeChart chart = ChartFactory.createBarChart("Case Statistics By Month",
	                 "Month", "Cases", dataset, PlotOrientation.VERTICAL, true,
	                 true, false);
	         try {
	             ChartUtilities.saveChartAsPNG(new File(path+"/CaseStatisticsByMonth.png"), chart, 1000, 500);
	             } catch (IOException e) {
	             System.err.println("Problem occurred creating chart.");
	         }
	         return path;
		 }
	/*  public String documentStatistics(){
		   DocumentStatistics ds=new DocumentStatistics();
		   DefaultPieDataset pieDataset = new DefaultPieDataset();
		 //  pieDataset.setValue("Total Documents="+ds.getNumberOfDocuments(),ds.getNumberOfDocuments());
		/*   pieDataset.setValue("Documents without Data Files="+ds.getNumberOfDocumentsByCategoryExcludingDataFiles(),ds.getNumberOfDocumentsByCategoryExcludingDataFiles());
		   pieDataset.setValue("Documents By Category And Type Excluding DataFiles="+ds.getNumberOfDocumentsByCategoryAndTypeExcludingDataFiles(),ds.getNumberOfDocumentsByCategoryAndTypeExcludingDataFiles());
		   pieDataset.setValue("Documents By Month="+ds.getNumberOfDocumentsByMonth(),ds.getNumberOfDocumentsByMonth());
		   pieDataset.setValue("Total Documents="+ds.getNumberOfDocuments(),ds.getNumberOfDocuments());

		   String path = "//home//koshyarb//Server//Tomcat//apache-tomcat-6.0.14//webapps//GAJE_BibbCounty_2.0_ScratchPad";
		    
		   final JFreeChart chart = ChartFactory.createPieChart("DocumentStatisticsChart",pieDataset, true,true,false);
	        
	         //saving the chart as png file
	         try {
	             ChartUtilities.saveChartAsPNG(new File(path+"/DocumentsStatisticsChart.png"), chart, 600,
	                 500);
	         }
	             catch (Exception e) {
	                 System.out.println("Problem occurred creating chart.");
	             }	
	             	return path;
	
		   
	   }*/
	 
	   public String documentStatisticsByMonth(){
	        DocumentStatistics ds = new DocumentStatistics();
	        // Create a simple Bar chart
	        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	        List<DocumentsCountPerMonth> monthList = new ArrayList<DocumentsCountPerMonth>();
	        monthList = ds.getNumberOfDocumentsByMonth();
	        for(DocumentsCountPerMonth documentsCountPerMonth : monthList){
	        	dataset.setValue(documentsCountPerMonth.getCount(), "Cases",documentsCountPerMonth.getMonth());
	        }
	         String path = getReportImagePath();
	        //String path = "//home//koshyarb//workspace//GAJE_BibbCounty_2.0_ScratchPad";
	        JFreeChart chart = ChartFactory.createBarChart("Documents Statistics By Month",
	                "Month", "Documents", dataset, PlotOrientation.VERTICAL, true,
	                true, false);
	        try {
	            ChartUtilities.saveChartAsPNG(new File(path+"/DocumentsStatisticsByMonth.png"), chart, 1000, 500);
	            } catch (IOException e) {
	            System.err.println("Problem occurred creating chart.");
	        }
	        return path;
			
		}
	   
	 
	   public String personStatistics(){
		   PersonStatistics ps=new PersonStatistics();
		   // Create Pie chart
		   DefaultPieDataset pieDataset = new DefaultPieDataset();
		   pieDataset.setValue("Number of Children Effected="+ps.getNumberOfChildren(),ps.getNumberOfChildren());
		   pieDataset.setValue("Number Of Defendents="+ps.getNumberOfDefendents(),ps.getNumberOfDefendents());
		   String path = getReportImagePath();
		  // String path = "//home//koshyarb//workspace//GAJE_BibbCounty_2.0_ScratchPad";
		   final JFreeChart chart = ChartFactory.createPieChart("Person Statistics Chart",pieDataset, true,true,false);
     
      //saving the chart as png file
      try {
    	  ChartUtilities.saveChartAsPNG(new File(path+"/PersonStatistics.png"),chart, 500, 400);
          	
           }
          catch (Exception e) {
              System.out.println("Problem occurred creating chart.");
          }	
      
            return "path";

	   
}
     //Signature Statistics Chart
	   
	   public String signatureStatistics(){
		   SignatureStatistics ss=new SignatureStatistics();
		   DefaultPieDataset pieDataset = new DefaultPieDataset();
		  // pieDataset.setValue("Total Signatures="+ss.getNumberOfSignatures(),ss.getNumberOfSignatures());
		   pieDataset.setValue("Signatures By Clerk="+ss.getNumberOfSignatureByCourtClerk(), ss.getNumberOfSignatureByCourtClerk());
		   pieDataset.setValue("Signatures By Attorney="+ss.getNumberOfSignaturesByAttorney(),ss.getNumberOfSignaturesByAttorney());
		   pieDataset.setValue("Signatures By Judge="+ss.getNumberOfSignaturesByJudge(),ss.getNumberOfSignaturesByJudge());
		   String path = getReportImagePath();
		  // String path = "//home//koshyarb//workspace//GAJE_BibbCounty_2.0_ScratchPad";
		   final JFreeChart chart = ChartFactory.createPieChart("Signature Statistics Chart",pieDataset, true,true,true);
   
    //saving the chart as png file
        try {
        	ChartUtilities.saveChartAsPNG(new File(path+"/SignatureStatisticsChart.png"), chart, 500,
            400);
         }
        catch (Exception e) {
            System.out.println("Problem occurred creating chart.");
         }	
        	return path;

	   
         }
    //Filing Statistics Chart
	   
	   public String filingStatistics(){
		   FilingStatistics fs=new FilingStatistics();
		   CaseStatistics caseStatusReport = new CaseStatistics();
		   DefaultPieDataset pieDataset = new DefaultPieDataset();

		  int UnprocessedFilings=caseStatusReport.getNumberOfCases()-fs.getNumberOfProcessedFiling();
	      // System.out.println("up="+UnprocessedFilings);
		   pieDataset.setValue("Number Of Unprocessed Filings="+UnprocessedFilings, UnprocessedFilings);
		   pieDataset.setValue("Number Of Processed Filings="+fs.getNumberOfProcessedFiling(),fs.getNumberOfProcessedFiling());
		   String path = getReportImagePath();
		   final JFreeChart chart = ChartFactory.createPieChart("Filing Statistics Chart",pieDataset, true,true,false);
		  
   
    //saving the chart as png file
          try {
        	  ChartUtilities.saveChartAsPNG(new File(path+"/FilingStatisticsChart.png"), chart, 500, 400);
           }
          catch (Exception e) {
        	  System.out.println("Problem occurred creating chart.");
           }	
        	return path;

	   
         }
        
	   public String systemStatistics(){
		  SystemStatistics sysStat=new SystemStatistics();
		   DefaultPieDataset pieDataset = new DefaultPieDataset();
		   pieDataset.setValue("Number Of Cases="+sysStat.getCaseStat(),sysStat.getCaseStat());
		   pieDataset.setValue("Number Of Persons="+sysStat.getPersonStat(),sysStat.getPersonStat());
		   pieDataset.setValue("Number Of Documents="+ sysStat.getDocumentStat(),sysStat.getDocumentStat());
		   pieDataset.setValue("Number Of Signatures="+sysStat.getSignatureStat(),sysStat.getSignatureStat());
		   pieDataset.setValue("Number Of Filings="+ sysStat.getFilingStat(),sysStat.getFilingStat());
		   String path = getReportImagePath();
		   final JFreeChart chart = ChartFactory.createPieChart("System Statistics Chart",pieDataset, true,true,false);
		  
   
    //saving the chart as png file
          try {
        	  ChartUtilities.saveChartAsPNG(new File(path+"/SytemStatisticsChart.png"), chart, 600, 500);
           }
          catch (Exception e) {
        	  System.out.println("Problem occurred creating chart.");
           }	
        	return path;

	   
         }


}
